var config = require('../../script/config');
var revenueApi = require('../../script/revenue.js');
import {
  parseTime
} from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: null,
    type: '0',
    dataList: [],
    TotalCount: 0,
    DeferCount: 0,
    DoneCount: 0,
    DoingCount: 0,
    CurrentPage: 1,
    PageSize: 20,
    HasNextPage: true,
    noData: false,
    isReachBottom: false,
    SearchPop: false,
    SearchParams: {
      DEPT_NAME: '',
      DEPT_PART: '',
      ROOM: '',
      UserCode: '',
      UserName: '',
      dateStart: '',
      dateEnd: '',
    },
    TaskAddress: [],
    date_s: '',
    date_e: '',
    DateS_Pop: false,
    DateE_Pop: false,
    DeptPop: false,
    DeptActive: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getDate();
    this.getUserInfo();
    this.getCount();
    this.getDataList(true);
    this.getAddress();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    this._refreshData();
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      isReachBottom: true
    });
    if (this.data.HasNextPage) {
      this.setData({
        CurrentPage: this.data.CurrentPage + 1
      })
      this.getDataList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  _refreshData() {
    this.setData({
      SearchParams: {
        DEPT_NAME: '',
        DEPT_PART: '',
        ROOM: '',
        UserCode: '',
        UserName: '',
        dateStart: '',
        dateEnd: '',
      },
      CurrentPage: 1,
      HasNextPage: true,
      noData: false,
      isReachBottom: false,
    });
    this.getDate();
    this.getCount();
    this.getDataList(true);
  },
  getUserInfo() {
    try {
      var value = wx.getStorageSync(config.storageKey.userinfo)
      if (value) {
        this.setData({
          userinfo: value
        })
      }
    } catch (e) {
      // Do something when catch erro
    }
  },
  getDate() {
    const date = new Date();
    const date_e = date.getTime();
    const dateEnd = parseTime(date, 'yyyy-MM-dd');
    date.setDate(date.getDate() - 6);
    const dateStart = parseTime(date, 'yyyy-MM-dd');
    const date_s = date.getTime();
    this.setData({
      date_s,
      "SearchParams.dateStart": dateStart,
      "SearchParams.dateEnd": dateEnd,
      date_e
    });
  },
  getAddress() {
    const params = {
      Data: JSON.stringify({
        AUDIT_ID: '',
        AUDIT_TYPE: 2,
        PlanState: 0
      }),
    }
    revenueApi.GetTaskAddress(params).then(res => {
      if (res.code === 200) {
        this.setData({
          TaskAddress: res.data || [],
        });
      }
    })
  },
  tabClick(e) {
    this.setData({
      type: e.currentTarget.dataset.type,
    })
    this.getDataList(true);
  },
  getCount() {
    const params = {
      Data: JSON.stringify({
        SelectedDateType: 0,
        dateStart: this.data.SearchParams.dateStart,
        dateEnd: this.data.SearchParams.dateEnd,
        DEPT_PART: this.data.SearchParams.DEPT_PART,
        ROOM: this.data.SearchParams.ROOM,
        UserCode: this.data.SearchParams.UserCode,
        UserName: this.data.SearchParams.UserName,
      }),
    }
    revenueApi.QueryHistoryCount(params).then(res => {
      if (res.code === 200) {
        this.setData({
          TotalCount: res.data.TotalCount,
          DeferCount: res.data.DeferCount,
          DoneCount: res.data.DoneCount,
          DoingCount: res.data.DoingCount
        });
      }
    })
  },
  getDataList(isRefresh) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    const params = {
      Data: JSON.stringify({
        AUDIT_TYPE: parseInt(this.data.type),
        AUDIT_NO: this.data.userinfo.USER_NO,
        CurrentPage: this.data.CurrentPage,
        PageSize: this.data.PageSize,
        SelectedDateType: 0,
        dateStart: this.data.SearchParams.dateStart,
        dateEnd: this.data.SearchParams.dateEnd,
        DEPT_PART: this.data.SearchParams.DEPT_PART,
        ROOM: this.data.SearchParams.ROOM,
        UserCode: this.data.SearchParams.UserCode,
        UserName: this.data.SearchParams.UserName,
      }),
    }
    revenueApi.QueryHistoryDetail(params).then(res => {
      wx.hideLoading();
      if (res.code === 200) {
        let dataList = []
        if (isRefresh) {
          dataList = [...res.data.DataList]
        } else {
          dataList = [...this.data.dataList, ...res.data.DataList]
        }
        let HasNextPage = res.data.HasNextPage;
        dataList.forEach(item => {
          const now = new Date().getTime();
          const end = new Date(item.END_DATE_Str).getTime();
          item.IsExpire = now > end;
          item.ADD_TIME_Str = item.ADD_TIME_Str.slice(0, 10)
        })
        const noData = dataList.length === 0;
        this.setData({
          dataList,
          HasNextPage,
          noData
        })
      }
    }).catch(_err => {
      wx.hideLoading();
    });
  },
  toDetail(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageAudit/order-detail/order-detail?id=" + id
    })
  },
  openSearch() {
    this.setData({
      SearchPop: true
    });
  },
  inputChange(e) {
    const type = e.currentTarget.dataset.type;
    let val = e.detail.value;
    this.setData({
      ['SearchParams.' + type]: val
    });
  },
  openDatePop(e) {
    var type = e.currentTarget.dataset.type;
    this.setData({
      [type + 'Pop']: true
    });
  },
  closeDatePop() {
    this.setData({
      DateS_Pop: false,
      DateE_Pop: false
    });
  },
  onInput_S(e) {
    this.setData({
      date_s: e.detail,
    });
  },
  onConfirm_S(e) {
    this.setData({
      DateS_Pop: false,
      "SearchParams.dateStart": parseTime(this.data.date_s, 'yyyy-MM-dd')
    });
    this.doSearch();
  },
  onInput_E(e) {
    this.setData({
      date_e: e.detail,
    });
  },
  onConfirm_E(e) {
    this.setData({
      DateE_Pop: false,
      "SearchParams.dateEnd": parseTime(this.data.date_e, 'yyyy-MM-dd')
    });
    this.doSearch();
  },
  openDeptPop(){
    this.setData({
      DeptPop: true
    });
  },
  closeDeptPop(){
    this.setData({
      DeptPop: false
    });
  },
  DeptConfirm(e) {
    const {
      value,
      index
    } = e.detail;
    this.setData({
      'SearchParams.DEPT_PART': value.DEPT_PART,
      'SearchParams.DEPT_NAME': value.DEPT_PART_Str,
      DeptPop: false,
      DeptActive: index
    });
  },
  reset() {
    this.setData({
      SearchParams: {
        DEPT_NAME: '',
        DEPT_PART: '',
        ROOM: '',
        UserCode: '',
        UserName: '',
        dateStart: '',
        dateEnd: '',
      },
    })
  },
  doSearch() {
    this.setData({
      CurrentPage: 1,
      HasNextPage: true,
      noData: false,
      isReachBottom: false,
      ShowPop: false
    });
    this.getCount();
    this.getDataList(true);
  },
})